/*
 *  Copyright (c) 2009 Baturu.COM
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package com.baturu.oss.dao.jpa;

import com.baturu.oss.dao.OpenUserDAO;
import com.baturu.oss.entities.users.OpenUser;
import org.sitevelocity.exceptions.NoSuchEntityException;
import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

/**
 *
 * @author <a href="mailto:javafuns@baturu.com">javafuns</a>
 */
public class OpenUserDAOImpl implements OpenUserDAO {
    private PersistenceManager pm;

    public OpenUserDAOImpl(PersistenceManager pm) {
        this.pm = pm;
    }

    public PersistenceManager getPersistenceManager() {
        return this.pm;
    }

    @Override
    public OpenUser newUser(OpenUser openUser) {
        return this.getPersistenceManager().makePersistent(openUser);
    }

    @Override
    public void updateUser(OpenUser openUser) throws NoSuchEntityException {
        OpenUser ou = this.getUser(openUser.getId());
        if (ou == null) {
            throw new NoSuchEntityException("User with ID [" + openUser.getId() + "] not exists.");
        }
        this.getPersistenceManager().makePersistent(openUser);
    }

    @Override
    public void removeUser(String id) {
        this.getPersistenceManager().deletePersistent(this.getUser(id));
    }

    @Override
    public List<OpenUser> findUsers(int maxResults, int firstResult, String orderByDesc) {
        Query query = this.getPersistenceManager().newQuery("select from " + OpenUser.class.getName());
        if (maxResults >= 0) {
            if (firstResult < 0) {
                firstResult = 0;
            }
            query.setRange(firstResult, firstResult + maxResults);
        }
        if (orderByDesc == null || "".equals(orderByDesc)) {
            orderByDesc = "id asc";
        }
        query.setOrdering(orderByDesc);
        List<OpenUser> ous = (List<OpenUser>) query.execute();
        return (ous==null?new ArrayList<OpenUser>(0):ous);
    }

    @Override
    public OpenUser getUser(String id) {
        return this.getPersistenceManager().getObjectById(OpenUser.class, id);
    }

    @Override
    public int getUserCount() {
        String querySQL = "select count(id) from " + OpenUser.class.getName();
        Query query = this.getPersistenceManager().newQuery(querySQL);
        Number count = (Number) query.execute();
        return count.intValue();
    }

    @Override
    public OpenUser getUserByLogin(String userLogin) {
        Query query = this.getPersistenceManager().newQuery("select from "
                + OpenUser.class.getName()
                + " where userLogin == userLoginParam"
                + " parameters String userLoginParam");
        List<OpenUser> users = (List<OpenUser>)query.execute(userLogin);
        return (users!=null&&!users.isEmpty()) ? users.get(0) : null;
    }

}
